#include"filesys.h"
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<dirent.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>


void fsLs(char *curPath,char *ls_info){
    DIR *dp;
    struct dirent *entry;
    int idx = 0;
    if((dp = opendir(curPath)) == NULL){
        if(errno == ENOENT){
            printf("no such file or directory\n");
            return;
        }
        else {
            printf("unable open directory");
            return;
        }
    }

    while((entry = readdir(dp)) != NULL){
        if(strcmp(entry->d_name,".") == 0 || strcmp(entry->d_name,"..") == 0)
            continue;
        if(entry->d_type == DT_DIR)
            idx += sprintf((char*)&ls_info[idx],"%s/\n",entry->d_name);
        else
            idx += sprintf((char*)&ls_info[idx],"%s\n",entry->d_name);
    }
    ls_info[idx] = '\n';
}




char *mergePath(char *root_dir, char *rlat_dir){
    char *newDir = (char*)malloc(sizeof(char) * DIR_MAX_SIZE);
    strcat(newDir,root_dir);
    if(rlat_dir[0] != '/')
        strcat(newDir,"/");
    strcat(newDir,rlat_dir);
    return newDir;
}

